<script lang="ts">
import {defineComponent} from "vue"

/**
 * 秒数转换为可读时间
 */
export default defineComponent({
  name: "FormatSeconds",
  props: {
    seconds: {
      type: Number,
      required: true
    }
  },
  setup(props) {
    return function () {
      if (props.seconds < 1) {
        return '';
      } else if (props.seconds < 60) {
        return props.seconds + '秒'
      } else if (props.seconds < 3600) {
        let minute = Math.floor(props.seconds / 60);
        let seconds = props.seconds % 60;
        return seconds ? `${minute}分${seconds}秒` : `${minute}分钟`
      } else if (props.seconds > 3600) {
        let hour = Math.floor(props.seconds / 3600);
        let minute = Math.floor((props.seconds % 3600) / 60);
        let seconds = props.seconds - (hour * 3600) - (minute * 60)
        if (hour && minute && seconds) {
          return `${hour}时${minute}分${seconds}秒`
        } else if (hour && minute) {
          return `${hour}时${minute}分`
        } else if (hour && seconds) {
          return `${hour}时${seconds}秒`
        }
      }
      return '';
    }
  }
})
</script>